package mhys.View.Teacher;
import mhys.Dao.LoginDao;
import mhys.Entity.User;
import mhys.Util.SendMailUtil;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import static java.awt.Toolkit.getDefaultToolkit;

public class FindPwd {
    LoginDao loginDao = new LoginDao();
    SendMailUtil sendMailUtil = new SendMailUtil();
    Image icon = getDefaultToolkit().getImage("D:/download/warriors.png");
    public FindPwd() {
        JFrame jFrame = new JFrame();
        jFrame.setTitle("找回密码");
        jFrame.setSize(400, 320);
        jFrame.setLocationRelativeTo(null);
        jFrame.setDefaultCloseOperation(3);
        jFrame.setLayout(null);
        jFrame.setIconImage(icon);

        JLabel title = new JLabel("找回密码");
        Font font = new Font("黑体",Font.PLAIN,22);
        title.setFont(font);
        title.setBounds(160, 10, 200, 30);

        JLabel emailLabel = new JLabel("邮箱：");
        emailLabel.setBounds(30, 60, 100, 30);
        JTextField emailField = new JTextField();
        emailField.setBounds(130, 60, 200, 30);

        JLabel pwdLabel = new JLabel("密码：");
        pwdLabel.setBounds(30, 100, 50, 30);
        JTextField pwdField1 = new JTextField();
        pwdField1.setBounds(130, 100, 200, 30);

        JLabel pwdLabel2 = new JLabel("再次输入密码：");
        pwdLabel2.setBounds(30, 140, 110, 30);
        JTextField pwdField2 = new JTextField();
        pwdField2.setBounds(130, 140, 200, 30);

        JLabel codeLabel = new JLabel("验证码：");
        codeLabel.setBounds(30, 180, 70, 30);
        JTextField codeField = new JTextField();
        codeField.setBounds(130, 180, 90, 30);
        JButton sendButton = new JButton("发送");
        sendButton.setBounds(240, 180, 90, 30);

        JButton resetButton = new JButton("重置");
        resetButton.setBounds(130, 220, 90, 30);

        JButton goButton = new JButton("去登录");
        goButton.setBounds(240, 220, 90, 30);

        resetButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String email = emailField.getText();
                String newPassword1 = pwdField1.getText();
                String newPassword2 = pwdField2.getText();
                String code1 = codeField.getText();
                User user = loginDao.findUser(email);
                if (user == null) {
                    JOptionPane.showMessageDialog(jFrame,"邮箱不存在！");
                }else {
                    if (!newPassword1.equals(newPassword2)){
                        JOptionPane.showMessageDialog(jFrame,"两次密码不一致，请重新输入！");
                    }else {
                        if (newPassword1.equals(newPassword2) && code1.equals(SendMailUtil.code2)){
                            loginDao.resetPassword(newPassword2);
                            JOptionPane.showMessageDialog(jFrame,"重置成功！");
                        }
                    }
                }
            }
        });
        sendButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String from_email = "1605030389@qq.com";
                String pwd = "heanakkebhbibadg";
                String email = emailField.getText();
                sendMailUtil.sendQQEmail(from_email,pwd,email,SendMailUtil.getCode());
                JOptionPane.showMessageDialog(jFrame,"验证码已发送！");
            }
        });
        goButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new Login();
                jFrame.dispose();
            }
        });

        jFrame.add(sendButton);
        jFrame.add(codeField);
        jFrame.add(codeLabel);
        jFrame.add(pwdField2);
        jFrame.add(pwdLabel2);
        jFrame.add(pwdField1);
        jFrame.add(title);
        jFrame.add(emailLabel);
        jFrame.add(emailField);
        jFrame.add(pwdLabel);
        jFrame.add(resetButton);
        jFrame.add(goButton);
        jFrame.setVisible(true);
    }

}
